load("agg_day_year_all.rdata")
load("n_day_year_all.rdata")

## commented out for replication files 
# install.packages("xtable")
# install.packages("ggplot2")
# install.packages("dplyr")
# install.packages("rmeta")

library(xtable)
library(ggplot2)
library(dplyr)
library(rmeta)

# descriptive table -------------------------------------------------------

## subset to 183 days +/-
data_day_year <- 
  data_day_year %>%
  filter(abs(days) < 183)
data_n_year <- 
  data_n_year %>%
  filter(abs(days) < 183)

data_day_year <- 
  within(data_day_year, {
    par_age[year == 2009] <- par_age[year == 2009] - 1463 
    par_age[year == 2014] <- par_age[year == 2014] + 187 
    par_age[year == 2015] <- par_age[year == 2015] + 577 
  })

# Create table with means on either side of threshold 

data_day_year <- left_join(data_day_year, data_n_year)

desc_table <- matrix(NA, 9, 10)

desc_table[1,9] <- with(subset(data_day_year, days < 0), 
                        weighted.mean(x = par_vote, w = n))
desc_table[1,10] <- with(subset(data_day_year, days > 0), 
                         weighted.mean(x = par_vote, w = n))

desc_table[2,9] <- with(subset(data_day_year, days < 0), 
                        weighted.mean(x = par_native, w = n))
desc_table[2,10] <- with(subset(data_day_year, days > 0), 
                         weighted.mean(x = par_native, w = n))

desc_table[3,9] <- with(subset(data_day_year, days < 0), 
                        weighted.mean(x = par_age, w = n))/365.25 + 18
desc_table[3,10] <- with(subset(data_day_year, days > 0), 
                         weighted.mean(x = par_age, w = n))/365.25 + 18

desc_table[4,9] <- with(subset(data_day_year, days < 0), 
                        weighted.mean(x = par_income, w = n))/7462.5
desc_table[4,10] <- with(subset(data_day_year, days > 0), 
                         weighted.mean(x = par_income, w = n))/7462.5

desc_table[5,9] <- with(subset(data_day_year, days < 0), 
                        weighted.mean(x = par_female, w = n))
desc_table[5,10] <- with(subset(data_day_year, days > 0), 
                         weighted.mean(x = par_female, w = n))

desc_table[6,9] <- with(subset(data_day_year, days < 0), 
                        weighted.mean(x = par_degree, w = n))
desc_table[6,10] <- with(subset(data_day_year, days > 0), 
                         weighted.mean(x = par_degree, w = n))

desc_table[7,9] <- with(subset(data_day_year, days < 0), 
                        weighted.mean(x = chi_female, w = n)) 
desc_table[7,10] <- with(subset(data_day_year, days > 0), 
                         weighted.mean(x = chi_female, w = n)) 

desc_table[8,9] <- with(subset(data_day_year, days < 0), 
                        weighted.mean(x = chi_oldest, w = n)) 
desc_table[8,10] <- with(subset(data_day_year, days > 0), 
                         weighted.mean(x = chi_oldest, w = n)) 

desc_table[9,9] <- with(subset(data_day_year, days < 0), 
                        sum(n))
desc_table[9,10] <- with(subset(data_day_year, days > 0), 
                         sum(n))

year <- c(2009, 2013, 2014, 2015)
for (i in 1:4){
  sub_year <- 
    data_day_year %>%
    filter(year == year[i])
  desc_table[1,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   weighted.mean(x = par_vote, w = n))
  desc_table[1,(2*i)] <- with(subset(sub_year, days > 0), 
                              weighted.mean(x = par_vote, w = n))
  
  desc_table[2,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   weighted.mean(x = par_native, w = n))
  desc_table[2,(2*i)] <- with(subset(sub_year, days > 0), 
                              weighted.mean(x = par_native, w = n))
  
  desc_table[3,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   weighted.mean(x = par_age, w = n))/365.25 + 18
  desc_table[3,(2*i)] <- with(subset(sub_year, days > 0), 
                              weighted.mean(x = par_age, w = n))/365.25 + 18
  
  desc_table[4,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   weighted.mean(x = par_income, w = n))/7462.5
  desc_table[4,(2*i)] <- with(subset(sub_year, days > 0), 
                              weighted.mean(x = par_income, w = n))/7462.5
  
  desc_table[5,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   weighted.mean(x = par_female, w = n))
  desc_table[5,(2*i)] <- with(subset(sub_year, days > 0), 
                              weighted.mean(x = par_female, w = n))
  
  desc_table[6,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   weighted.mean(x = par_degree, w = n))
  desc_table[6,(2*i)] <- with(subset(sub_year, days > 0), 
                              weighted.mean(x = par_degree, w = n))
  
  desc_table[7,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   weighted.mean(x = chi_female, w = n)) 
  desc_table[7,(2*i)] <- with(subset(sub_year, days > 0), 
                              weighted.mean(x = chi_female, w = n)) 
  
  desc_table[8,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   weighted.mean(x = chi_oldest, w = n)) 
  desc_table[8,(2*i)] <- with(subset(sub_year, days > 0), 
                              weighted.mean(x = chi_oldest, w = n)) 
  
  desc_table[9,(-1 + 2*i)] <- with(subset(sub_year, days < 0), 
                                   sum(n))
  desc_table[9,(2*i)] <- with(subset(sub_year, days > 0), 
                              sum(n))
}

desc_table[c(1,2,5,6,7,8),] <- desc_table[c(1,2,5,6,7,8),] * 100
rownames(desc_table) <- c("Parents Voted (%)",
                          "Parents Native (%)",
                          "Parents' Age (Years)",
                          "Parents' Income (1000 euro)",
                          "Parents Female (%)",
                          "Parents Degree (%)",
                          "Child Female (%)",
                          "Child Oldest (%)",
                          "N")

colnames(desc_table) <- rep(c("Ineligible",
                              "Eligible")  ,5)

dig_mat <- matrix(c(rep(0, 9), 
                    rep(c(rep(2,8),0),10)), nrow = 9, ncol = 11)

desc_out <- 
  xtable(caption = "Means within six months of cutoff",
         desc_table, digits = dig_mat)
desc_out
